package dao

import (
	"RestaurantApp/model"
	"strconv"
	"time"
)

type MemberDao struct {
}

func (this *MemberDao) QueryMemberByUsernameAndPassword(username, password string) model.Member {

	queryWrapper := model.Member{
		UserName: username,
		Password: password,
	}

	member := model.Member{}
	model.DB.Where(&queryWrapper).First(&member)

	return member

}

func (this *MemberDao) AddMemberByUsernameAndPassword(username, password string) model.Member {
	member := model.Member{
		UserName:     username,
		Password:     password,
		RegisterTime: time.Now(),
	}
	model.DB.Create(&member)
	queryWrapper := model.Member{
		UserName: username,
		Password: password,
	}
	resp := model.Member{}

	model.DB.Where(queryWrapper).First(resp)

	return resp

}

func (this *MemberDao) UpdateAvatorUrlByUserId(url, userId string) {
	id, _ := strconv.Atoi(userId)
	queryWrapper := model.Member{
		Id: int64(id),
	}
	model.DB.Model(&model.Member{}).Where(queryWrapper).Update("avatar", url)

}

func (this *MemberDao) GetUserInfoByUserId(userId int) model.Member {
	queryWrapper := model.Member{
		Id: int64(userId),
	}
	member := model.Member{}
	model.DB.Where(queryWrapper).First(&member)
	return member
}
